<template>
  <div>
    <el-dialog
      :title="isEdit ? '修改角色': '新增角色'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :visible="showDialog"
      @closed="closedFn"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="roleRules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>

      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="cancleRoles">取消</el-button>
          <el-button size="small" type="primary" @click="roleSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'SettingDialog',
    props: {
      // dialog弹出框控制值
      showDialog: {
        type: Boolean,
        default: false
      },
      // 弹出层共用(新增/修改)
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 添加角色
        roleForm: {
          name: '',
          description: ''
        },
        // 添加角色验证
        roleRules: {
          name: [
            { required: true, message: '角色名称不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '角色描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // ...其他
      // 角色弹框-> 确定按钮
        roleSubmit() {
          this.$refs.roleForm.validate(valid => {
            // 无论是新增/编辑,做的事情都是一样,发送表单对象出去,关闭dialog弹窗
            if(valid){
              this.$emit('addRole',this.roleForm) // 派发表单对象内容给父组件发请求
              this.$emit('update:showDialog',false) // 关闭dialog
            }
          })
        },
        // 角色弹框-> 取消按钮
        cancleRoles() {
          this.$emit('update:showDialog',false)
        },
        // 清空表单数据
        closedFn(){
          this.$nextTick(() => {
            this.$refs.roleForm.resetFields()
          })
        }
    }
  }
</script>

<style>
</style>
